﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSDAL;

namespace MBSBusinessEntities
{
    public class ShowTimeBE : ICrud
    {

          public ShowTimeBE()
        {
            mbs = new MBS_Entities();
        }

        public ShowTimeBE (
            int ShowID,
          double Fare,
          DateTime ShowDateTime,    
          string SeatsAvailable,
          int MovieScheduleID,
            bool IsDeleted)
          {
              mbs = new MBS_Entities();
              this.ShowID = ShowID;
              this.Fare = Fare;
              this.ShowDateTime = ShowDateTime;
              this.SeatsAvailable = SeatsAvailable;
              this.MovieScheduleID = MovieScheduleID;
              this.IsDeleted = IsDeleted;
    }

        #region ClassVariables
          private MBS_Entities mbs;     
          private int showID;
          private double fare;      
          private DateTime showDateTime;     
          private string seatsAvailable;
          private int movieScheduleID;
          private bool isDeleted;
          private bool isValid=true;

          public bool IsValid
          {
              get { return isValid; }
              set { isValid = value; }
          }
        #endregion


        #region Properties

          public MBS_Entities Mbs
          {
              get { return mbs; }
              set { mbs = value; }
          }

          public int ShowID
          {
              get { return showID; }
              set { showID = value; }
          }

          public double Fare
          {
              get { return fare; }
              set { fare = value; }
          }

          public DateTime ShowDateTime
          {
              get { return showDateTime; }
              set { showDateTime = value; }
          }

          public string SeatsAvailable
          {
              get { return seatsAvailable; }
              set { seatsAvailable = value; }
          }

          public int MovieScheduleID
          {
              get { return movieScheduleID; }
              set { movieScheduleID = value; }
          }

          public bool IsDeleted
          {
              get { return isDeleted; }
              set { isDeleted = value; }
          }

        #endregion


        #region Methods
          public bool updateSeatAvailable()
          {
              ShowTime showTime = GetShowTimeDALByID(this.ShowID);
              if (showTime == null) return false;
              showTime.SeatsAvailable = this.SeatsAvailable;
              int num = mbs.SaveChanges();
              return num > 0;
          }

          private bool Validate_Insertion()
          {
              if (this.MovieScheduleID <= 0) { return false; }

              return true;
          }

          public bool Add()
          {

              if (Validate_Insertion())
              {
                  ShowTime showTime = new ShowTime();
                  showTime.ShowID = this.ShowID;
                  showTime.Fare = Convert.ToDecimal(this.Fare);
                  showTime.ShowDateTime = this.ShowDateTime;
                  showTime.SeatsAvailable = this.SeatsAvailable;
                  showTime.MovieScheduleID = this.MovieScheduleID;
                  showTime.IsDeleted = this.IsDeleted;
                  mbs.AddToShowTimes(showTime);
                  mbs.SaveChanges();
                  return true;
              }

              return false;
          }

          public bool Update()
          {
              ShowTime showTime = GetShowTimeDALByID(this.ShowID);
              if (showTime == null) return false;
              
              showTime.Fare = Convert.ToDecimal(this.Fare);
              showTime.ShowDateTime = this.ShowDateTime;
              showTime.SeatsAvailable = this.SeatsAvailable;
              showTime.MovieScheduleID = this.MovieScheduleID;
              showTime.IsDeleted = this.IsDeleted;
              mbs.AddToShowTimes(showTime);
              mbs.SaveChanges();
              return true;
          }

          public bool Delete(int ShowTimeID)
          {
              ShowTime showTime = GetShowTimeDALByID(this.ShowID);
              return Delete(showTime);
          }

          private bool Delete(ShowTime showTime)
          {

               if (showTime == null) return false;

              showTime.Fare = Convert.ToDecimal(this.Fare);
              showTime.ShowDateTime = this.ShowDateTime;
              showTime.SeatsAvailable = this.SeatsAvailable;
              showTime.MovieScheduleID = this.MovieScheduleID;
              showTime.IsDeleted = true;
              mbs.AddToShowTimes(showTime);
              mbs.SaveChanges();

              return true;
          }
         
        public bool DeleteShowTimeOfSpecificMovieSchedule(int MovieScheduleID)
          {

            List <ShowTime> showTimeList = GetShowTimeDALByMovieScheduleID(MovieScheduleID);

            foreach (ShowTime st in showTimeList)
            {
                Delete(st);
            }

              return true;
          }
        

         

          private ShowTimeBE ConvertDALToBusinessEntity(ShowTime showTimeDAL)
          {
              if (showTimeDAL == null) return null;
              ShowTimeBE showTimeBE = new ShowTimeBE();
              showTimeBE.ShowID = showTimeDAL.ShowID;
              showTimeBE.Fare = Convert.ToDouble(showTimeDAL.Fare);
              showTimeBE.ShowDateTime = Convert.ToDateTime(showTimeDAL.ShowDateTime);
              showTimeBE.SeatsAvailable = showTimeDAL.SeatsAvailable;
              showTimeBE.MovieScheduleID = showTimeDAL.MovieScheduleID;
              showTimeBE.IsDeleted = showTimeDAL.IsDeleted;
              return showTimeBE;
          }

        
        // By ShowTimeID
          public object GetByID(int ShowTimeID)
          {
              ShowTimeBE showTimebe = new ShowTimeBE();
              ShowTime showTime = GetShowTimeDALByID(ShowTimeID);
              showTimebe = ConvertDALToBusinessEntity(showTime);
              if (Validate_Retrieval(showTimebe))
              {
                  return showTimebe;
              }

              return null;
          }

          

          private bool Validate_Retrieval(ShowTimeBE showTimebe)
          {
              if (showTimebe == null) { isValid = false; return false; }
              
              return true;
          }

          private ShowTime GetShowTimeDALByID(int ShowID)
          {
              ShowTime showTimeDAL = new ShowTime();
              try
              {
                  showTimeDAL = mbs.ShowTimes.First(x => x.ShowID == ShowID);
              }
              catch (System.InvalidOperationException ex)
              {
                  return null;
              }

              return showTimeDAL;

          }

        // By MovieID, CinemaID, & Screen No
          private List<ShowTime> GetShowTimeDALByMoviesAndCinemaAndTheatreNo(int MovieID, int CinemaID, int ScreenNo)
          {
              List<ShowTime> showTimeList = mbs.ShowTimes.Where(s => s.MovieSchedule.MovieID == MovieID && s.MovieSchedule.CinemaID == CinemaID && s.MovieSchedule.ScreenNo == ScreenNo && s.IsDeleted == false).ToList();

              return showTimeList;
          }

         public List<ShowTimeBE> GetShowTimeByMovieAndCinemaAndTheatreNo(int MovieID, int CinemaID, int ScreenNo){
           List<ShowTime> showTimeList = GetShowTimeDALByMoviesAndCinemaAndTheatreNo(MovieID, CinemaID, ScreenNo);
           List<ShowTimeBE> showTimeBEList = new List<ShowTimeBE>();
              foreach (ShowTime showTime in showTimeList)
           {
               ShowTimeBE showTimeBE = ConvertDALToBusinessEntity(showTime);
               if (showTimeBE!=null)
                showTimeBEList.Add(showTimeBE);
           }
             return showTimeBEList;
         
         }


         // By MovieID & CinemaID
         private List<ShowTime> GetShowTimeDALByMovieAndCinema(int MovieID, int CinemaID)
         {
             List<ShowTime> showTimeList = mbs.ShowTimes.Where(s => s.MovieSchedule.MovieID == MovieID && s.MovieSchedule.CinemaID == CinemaID && s.IsDeleted == false).ToList();

             return showTimeList;
         }

         public List<ShowTimeBE> GetShowTimeByMovieAndCinema(int MovieID, int CinemaID)
         {
             List<ShowTime> showTimeList = GetShowTimeDALByMovieAndCinema(MovieID, CinemaID);
             List<ShowTimeBE> showTimeBEList = new List<ShowTimeBE>();
             foreach (ShowTime showTime in showTimeList)
             {
                 ShowTimeBE showTimeBE = ConvertDALToBusinessEntity(showTime);
                 if (showTimeBE != null)
                     showTimeBEList.Add(showTimeBE);
             }
             return showTimeBEList;

         }


         // By MovieID, CinemaID, StartQueryDate, EndQueryDate
         private List<ShowTime> GetShowTimeDALByMovieCinemaStartDateAndEndDate(int MovieID, int CinemaID, DateTime StartQueryDate, DateTime EndQueryDate)
         {
             List<ShowTime> showTimeList = mbs.ShowTimes.Where(s => s.MovieSchedule.MovieID == MovieID && s.MovieSchedule.CinemaID == CinemaID && s.ShowDateTime>= StartQueryDate && s.ShowDateTime <= EndQueryDate && s.IsDeleted == false).ToList();
             
             return showTimeList;
         }

         public List<ShowTimeBE> GetShowTimeByMovieCinemaStartDateAndEndDate(int MovieID, int CinemaID, DateTime StartQueryDate, DateTime EndQueryDate)
         {
             List<ShowTime> showTimeList = GetShowTimeDALByMovieCinemaStartDateAndEndDate(MovieID, CinemaID, StartQueryDate, EndQueryDate);
             List<ShowTimeBE> showTimeBEList = new List<ShowTimeBE>();
             foreach (ShowTime showTime in showTimeList)
             {

                 ShowTimeBE showTimeBE = ConvertDALToBusinessEntity(showTime);
                 if (showTimeBE != null)
                    showTimeBEList.Add(showTimeBE);
             }
             return showTimeBEList;

         }

        public List<KeyValuePair<ShowTimeBE, int>> GetPairShowTimeByMovieCinemaStartDateAndEndDate(int MovieID, int CinemaID, DateTime StartQueryDate, DateTime EndQueryDate)
         {
             List<KeyValuePair<ShowTimeBE, int>> showTimeKVPairList = new List<KeyValuePair<ShowTimeBE, int>>();
             List<ShowTime> showTimeList = GetShowTimeDALByMovieCinemaStartDateAndEndDate(MovieID, CinemaID, StartQueryDate, EndQueryDate);
             
             foreach (ShowTime showTime in showTimeList)
             {
                 ShowTimeBE showTimeBE = ConvertDALToBusinessEntity(showTime);
                 if (showTimeBE != null)
                     showTimeKVPairList.Add(new KeyValuePair<ShowTimeBE, int>(showTimeBE, showTime.MovieSchedule.ScreenNo));
             }
             return showTimeKVPairList;

         }
        

        // by MovieScheduleID
         private List<ShowTime> GetShowTimeDALByMovieScheduleID(int MovieScheduleID)
         {
             List<ShowTime> showTimeList = mbs.ShowTimes.Where(s => s.MovieScheduleID == MovieScheduleID && s.IsDeleted == false).ToList();

             return showTimeList;
         }

         public List<ShowTimeBE> GetShowTimeByMovieAndCinemaAndTheatreNo(int MovieScheduleID)
         {
             List<ShowTime> showTimeList = GetShowTimeDALByMovieScheduleID(MovieScheduleID);
             List<ShowTimeBE> showTimeBEList = new List<ShowTimeBE>();
             foreach (ShowTime showTime in showTimeList)
             {
                 ShowTimeBE showTimeBE = ConvertDALToBusinessEntity(showTime);
                 if (showTimeBE != null)
                    showTimeBEList.Add(showTimeBE);
             }
             return showTimeBEList;

         }
        #endregion

    }
}
